package com.koron.housing.admin.service.impl;


import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.housing.admin.bean.po.HousingAdmin;
import com.koron.housing.admin.bean.query.HousingAdminQuery;
import com.koron.housing.admin.mapper.HousingAdminMapper;
import com.koron.housing.admin.service.HousingAdminService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 过渡房管理人员(HousingAdmin)表服务实现类
 *
 * @author jzp
 * @since 2022-02-26 17:59:40
 */
@Service("adminService")
public class HousingAdminServiceImpl implements HousingAdminService {

    private String getCurrAccount() {
        return ThreadLocalContext.get().getAccount();
    }

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public HousingAdmin getById(SessionFactory factory, String id) {
        HousingAdminMapper mapper = factory.getMapper(HousingAdminMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<HousingAdmin> list(SessionFactory factory, HousingAdminQuery query) {
        HousingAdminMapper mapper = factory.getMapper(HousingAdminMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("listHousingAdmin")
    public List<HousingAdmin> listHousingAdmin(SessionFactory factory) {
        HousingAdminQuery query = new HousingAdminQuery();
        // 1为过渡房管理人员
        query.setType("1");
        return list(factory, query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<HousingAdmin> page(SessionFactory factory, PageQuery pageQuery, HousingAdminQuery query) {
        pageQuery.prefixOrders("housingAdmin", null);
        return HousingAdminService.super.page(factory, pageQuery, query);
    }

    @Override
    @TaskAnnotation("isAdmin")
    public boolean isAdmin(SessionFactory factory, String account) {
        HousingAdminMapper mapper = factory.getMapper(HousingAdminMapper.class);
        HousingAdmin housingAdmin = mapper.selectByAccount(account);
        return housingAdmin != null;
    }

    @Override
    @TaskAnnotation("isAdminOfCurr")
    public boolean isAdminOfCurr(SessionFactory factory) {
        return isAdmin(factory, getCurrAccount());
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, HousingAdmin housingAdmin) {
        HousingAdminMapper mapper = factory.getMapper(HousingAdminMapper.class);
        return mapper.insertOrUpdate(housingAdmin) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, HousingAdmin housingAdmin) {
        HousingAdminMapper mapper = factory.getMapper(HousingAdminMapper.class);
        return mapper.update(housingAdmin) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        HousingAdminMapper mapper = factory.getMapper(HousingAdminMapper.class);
        return mapper.hidden(id) > 0;
    }
}
